<?php

namespace PAT\WishlistBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use PAT\WishlistBundle\Entity\Wishlist;

class WishlistController extends Controller {

    public function indexAction() {
        $user = $this->getCurrentUser();
        // si user nest pas authentifie on le renvoi sur index
        if (!is_object($user)) {
            return $this->redirect($this->generateUrl("index"));
        }

        $wishlistList = $user->getWishlists();

        $wishlist = new Wishlist();

        $form = $this->createAddForm($wishlist);

        $request = $this->get('request');

        if ($request->getMethod() == 'POST') {
            $form->bindRequest($request);

            if ($form->isValid()) {
                $wishlist->setUser($user);
                $em = $this->getDoctrine()->getEntityManager();
                $em->persist($wishlist);
                $em->flush();

                return $this->redirect($this->generateUrl("pat_wishlist_index"));
            }
        }
        return $this->render('PATWishlistBundle:Default:index.html.twig', array(
                    'form' => $form->createView(),
                    'wishlistList' => $wishlistList
                ));
    }

    public function getCurrentUser() {
        return $this->container->get("security.context")->getToken()->getUser();
    }

    public function createAddForm($wishlist) {
        return $this->createFormBuilder($wishlist)
                        ->add('name', 'text')
                        ->getForm();
    }

    public function deleteAction($id) {

        $em = $this->getDoctrine()->getEntityManager();
        $wishlist = $em->getRepository('PATWishlistBundle:Wishlist')->find($id);

        $em->remove($wishlist);
        $em->flush();

        return $this->redirect($this->generateUrl("pat_wishlist_index"));
    }

    public function displayAction($id) {
        $em = $this->getDoctrine()->getEntityManager();
        $wishlist = $em->getRepository('PATWishlistBundle:Wishlist')->find($id);

        return $this->render('PATWishlistBundle:Default:display.html.twig', array(
                    'wishlist' => $wishlist
                ));
    }

    public function chooseWishlistAction($productId, $wishlistId) {
        $em = $this->getDoctrine()->getEntityManager();
        $product = $em->getRepository('PATProductBundle:Product')->find($productId);
        $wishlist = $em->getRepository('PATWishlistBundle:Wishlist')->find($wishlistId);
        $wishlistProducts = $wishlist->getProducts();
        if ($wishlistProducts->contains($product)) {
            return $this->redirect($this->generateUrl('pat_product_display', array('id' => $productId)));
        } else {
            $wishlist->addProduct($product);
            $em->persist($wishlist);
            $em->flush();
        }

        return $this->redirect($this->generateUrl('pat_product_display', array('id' => $productId)));
    }

    public function deleteProductAction($wishlistId, $productId) {
        $em = $this->getDoctrine()->getEntityManager();
        $wishlist = $em->getRepository('PATWishlistBundle:Wishlist')->find($wishlistId);
        $product = $em->getRepository('PATProductBundle:Product')->find($productId);
        $wishlist->removeProduct($product);
        $em->persist($wishlist);
        $em->flush();

        return $this->redirect($this->generateUrl('pat_wishlist_display', array('id' => $wishlistId)));
    }
    
    public function sendMailAction($wishlistId){
        $wishlist = $this->getDoctrine()->getEntityManager()->getRepository('PATWishlistBundle:Wishlist')->find($wishlistId);
        /*$message = \Swift_Message::newInstance()
                ->setSubject('Ma wishlist: '+$wishlist->getName())
                ->setFrom($wishlist->getUser()->getEmail())
                ->setTo($wishlist->getUser()->getEmail())
                ->setBody($this->renderView('PATWishlistBundle:Default:mail.html.twig', array('wishlist' => $wishlist)));
        $this->get('mailer')->send($message);
        
        return $this->redirect($this->generateUrl("pat_wishlist_index")); */
        
        return $this->render('PATWishlistBundle:Default:mail.html.twig', array(
                    'wishlist' => $wishlist
                ));
    }
}